package com.escape.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.escape.subject.application.convert.SubjectAnswerDTOConvert;
import com.escape.subject.application.convert.SubjectInfoDTOConvert;
import com.escape.subject.application.dto.SubjectInfoDTO;
import com.escape.subject.common.entity.PageResult;
import com.escape.subject.common.entity.Result;
import com.escape.subject.domain.entity.SubjectAnswerBO;
import com.escape.subject.domain.entity.SubjectInfoBO;
import com.escape.subject.domain.service.SubjectInfoDomainService;
import com.escape.subject.infra.basic.entity.SubjectCategory;
import com.escape.subject.infra.basic.entity.SubjectInfo;
import com.escape.subject.infra.basic.service.SubjectCategoryService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
*@Author: escape_zebra
*@CreateTime: 2024-12-17  20:44
*@Description: TODO
*@Version: 1.0
*/
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.DTO: {}", JSON.toJSONString(subjectInfoDTO));
            }

            // 1. 参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名称不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectParse()),"题目解析不能为空!");

            // 2. DTO -> BO
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBO(subjectInfoDTO);
            if (subjectInfoDTO.getOptionList() != null) {
                List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConvert.INSTANCE.convertDtoListToBoList(subjectInfoDTO.getOptionList());
                subjectInfoBO.setOptionList(subjectAnswerBOS);
            }
            subjectInfoDomainService.add(subjectInfoBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error: {}", e.getMessage(), e);
            return Result.fail("新增题目失败!");
        }
    }

    /**
     * 分页查询题目列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<PageResult<SubjectInfoDTO>> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.DTO: {}", JSON.toJSONString(subjectInfoDTO));
            }

            //  1. 校验
            Preconditions.checkNotNull(subjectInfoDTO.getPageNo(),"页码不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签 id 不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类 id 不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"难度不能为空!");

            // 2 转换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBO(subjectInfoDTO);

            // 3. 查询
            PageResult<SubjectInfoBO> result =  subjectInfoDomainService.querySubjectInfo(subjectInfoBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error: {}", e.getMessage(), e);
            return Result.fail("查询题目列表失败!");
        }
    }

    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/queryDetail")
    public Result<SubjectInfoDTO> queryDetail(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectController.queryDetail.DTO: {}", JSON.toJSONString(subjectInfoDTO));
            }

            // 1. 校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目 id 不能为空!");

            // 2. 转换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConvert.INSTANCE.convertDtoToBO(subjectInfoDTO);

            SubjectInfoBO subjectInfoBORes = subjectInfoDomainService.queryDetail(subjectInfoBO);

            // 3. 转换
            SubjectInfoDTO dto = SubjectInfoDTOConvert.INSTANCE.convertBOToDto(subjectInfoBORes);

            return Result.ok(dto);
        }catch(Exception e){
            log.error("SubjectController.queryDetail.error: {}", e.getMessage(), e);
            return Result.fail("查询题目详情失败!");
        }
    }
}
